package org.halk;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.halk.entity.StatisticsClctSubject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

public class MyBatisApiDemo {

    public static void main(String[] args) throws SQLException, IOException {
        System.out.println("Hello, World!");

        // 获取 MyBatis 配置文件的 Reader 对象
        Reader reader2 = Resources.getResourceAsReader("mybatis-config.xml");

        InputStream resourceAsStream = MyBatisApiDemo.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        Reader reader = new InputStreamReader(resourceAsStream);


        // 创建 SqlSessionFactory 对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

        // 使用 SqlSessionFactory 创建 SqlSession 对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            // 执行查询，返回一个列表
            List<StatisticsClctSubject> subjects = sqlSession.selectList("org.halk.mapper.StatisticsClctSubjectMapper.selectAll");

            // 遍历列表并打印数据
            for (StatisticsClctSubject subject : subjects) {
                System.out.println(subject);
            }
        } finally {
            // 关闭 SqlSession
            sqlSession.close();
        }
    }
}
